//  @ Project : SSU Robot Simulator
//  @ File Name : ErrorManager.cpp
//  @ Date : 4/3/2011
//  @ Author : Andrew Holbrook
//
//

#include "SSURoboSim/ErrorLog/ErrorManager.h"
#include "SSURoboSim/ErrorLog/Error.h"

using SSURoboSim::ErrorLog::ErrorManager;
using SSURoboSim::ErrorLog::Error;

ErrorManager *ErrorManager::instance_ = new ErrorManager();

ErrorManager * ErrorManager::getInstance()
{
	return ErrorManager::instance_;
}

ErrorManager::~ErrorManager()
{
}

vector<string> ErrorManager::getSystemIds()
{
	map<string, vector<Error>>::iterator it;
	vector<string> v;

	for (it = errorMap_.begin(); it != errorMap_.end(); ++it)
	{
		v.push_back(it->first);
	}

	return v;
}

vector<Error> * ErrorManager::getErrors(string systemId)
{
	return &errorMap_[systemId];
}

bool ErrorManager::isError(string systemId)
{
	if (errorMap_[systemId].size())
	{
		return true;
	}

	return false;
}

void ErrorManager::addError(string systemId, Error &e)
{
	errorMap_[systemId].push_back(e);

	if (errorMap_[systemId].back().getErrorId() == -1)
	{
		errorMap_[systemId].back().setErrorId(errorMap_[systemId].size() - 1);
	}
}

void ErrorManager::update()
{
	map<string, vector<Error>>::iterator mapIt;
	vector<Error>::iterator vecIt;

	for (mapIt = errorMap_.begin(); mapIt != errorMap_.end(); ++mapIt)
	{
		for (vecIt = mapIt->second.begin(); vecIt != mapIt->second.end(); ++vecIt)
		{
			if (vecIt->getStatus() == Error::SOLVED)
			{
				mapIt->second.erase(vecIt);
			}
		}
	}
}

ErrorManager::ErrorManager()
{
}
